<?php 
class search extends Controller {

var $data_product	 = '';
var	$nid_products	 = '';

var $row_per_page	 = 12;
var $total_page		 = 0;

var	$title					= '';
var	$tags					= '';
var	$description			= ''; 

function search()
	{
		parent::Controller();
		session_start();
		$this->load->database();
		$this->load->helper('ap_function');
		$this->load->helper('ap_object');
		$this->load->helper('ap_view');
		$this->load->helper('ap_module');
		$this->load->helper('ap_html');
		$this->load->model('search_model');	
		$this->load->language('ap', 'eng');
	}
	
function tag($m_ctag = '',$currentpage = 1)
{
	$cproduct		= 'Từ khóa...';
	$nid_material	= 0;
	$nid_category 	= 0;
	$cbo_weight		= 1;
	$cbo_year		= 1;
	$ctag			= '';
	$where_str 		= '';
	$ctr 			='';	
	if($m_ctag != '')
	{
		$ctag		= $m_ctag;
		if($ctag != '')
			$where_str 		.= ' AND ctag like "%'.$ctag.'%" ' ;
		
		Fset_userdata('where_str',$where_str);
	}


	
	$current_page		= $currentpage; 
	$total_row 			= $this->search_model->get_count_list_product_search($where_str);
	$total_page			= Fget_total_page($this->row_per_page,$total_row);

	$data_list = $this->search_model->get_list_product_search($where_str,$total_row,$current_page,$this->row_per_page);
	$param					= '';
	$data['get_page']		= Fgen_pagination($current_page, $total_page, base_url().'search/keyword/', $param);
	$data['total_row']		= $total_row;
	$data['data_list'] 		= $data_list;
	
	$this->title			= 'Search product';
	$data['title']			= $this->title;
	$data['tags']			= $this->tags;
	$data['description']	= $this->description;
	$data['menu_top']		= 'search';
	$_SESSION['back_link']			= base_url().'index.php/search/keyword/';
	$this->load->view('search',$data);
}
	
function keyword($currentpage = 1)
{
	$cproduct		= 'Từ khóa...';
	$nid_material	= 0;
	$nid_category 	= 0;
	$cbo_weight		= 1;
	$cbo_year		= 1;
	$where_str 		= '';
	$str_sec		= '';
	$str_cat		= '';
	$chk_price		= '';
	$str_price		= '';
	$ctr			= '';
	$ctr =Fget_userdata('ctr');
	if($ctr !='search')
	{
		Fset_userdata('where_str','');
		Fset_userdata('keyword','');
	}
	
	if(isset($_POST['searchword']))
	{
		Fset_userdata('where_str','');
		Fset_userdata('str_sec','');
		Fset_userdata('str_sec','');
		Fset_userdata('str_price','');
		Fset_userdata('chk_price','');
		$cproduct		= $_POST['searchword'];
		if($cproduct != '' && $cproduct != 'Từ khóa...')
			$where_str 		.= ' AND cproducts like "%'.htmlentities ($cproduct).'%" ' ;		
		Fset_userdata('where_str',$this->db->escape_str($where_str));
		Fset_userdata('keyword',$cproduct);
	}else
	{
		$where_str 	=Fget_userdata('where_str');
		$cproduct	=Fget_userdata('keyword');
	}

	
	
	$str_sec	=Fget_userdata('str_sec');
	$str_sec = str_replace('_',',',$str_sec);
	if($str_sec != '' AND $str_sec != '0')
		$where_str 		.= ' AND nid_material_products IN ('.$str_sec.')' ;		

	$str_cat	=Fget_userdata('str_cat');
	$str_cat = str_replace('_',',',$str_cat);
	if($str_cat != '' AND $str_cat != '0')
		$where_str 		.= ' AND nid_cat_products IN ('.$str_cat.')' ;		
		
	$str_price =	Fget_userdata('str_price');
	if($str_price != '')
		$where_str 		.= $str_price;		
	$chk_price=Fget_userdata('chk_price');
	//exit($chk_price);
//exit(Fget_userdata('str_sec'));
	$current_page		= $currentpage; 
	$total_row 			= $this->search_model->get_count_list_product_search($where_str);
	$total_page			= Fget_total_page($this->row_per_page,$total_row);

	$data_list = $this->search_model->get_list_product_search($where_str,$total_row,$current_page,$this->row_per_page);
	$param					= '';
	//$data['get_page']		= Fgen_pagination($current_page, $total_page, base_url().'search/keyword/', $param);
	$data['get_page']		= Fgen_pagination2($currentpage,$total_page, base_url().'search/keyword/');
	$data['cproduct'] 		= $cproduct;
	$data['cbo_weight']		= $cbo_weight;
	$data['cbo_year']		= $cbo_year;
	$data['total_row']		= $total_row;
	$data['data_list'] 		= $data_list;
	$data['str_sec'] 		= $str_sec;
	$data['chk_price'] 		= $chk_price;
	
	$data['str_cat'] 		= $str_cat;

	$this->title			= 'Search product';
	$data['title']			= $this->title;
	$data['tags']			= $this->tags;
	$data['description']	= $this->description;
	$data['menu_sec']		= $nid_material;
	$data['menu_cat']		= $nid_category;
	$_SESSION['back_link']			= base_url().'index.php/search/keyword/';
	$data['g_ishome']				= 0;
	$this->load->view('search',$data);
}


function aSearchSec()
{
	Fset_userdata('str_sec','');
	$tmp  	= Fget_userdata('str_sec');
	$sec	= '';
	$ctr	= '';
	if(isset($_POST['sec']))
		$sec = $_POST['sec'];
	if(isset($_POST['ctr']))
		$ctr = $_POST['ctr'];

	if($sec != '')	
	foreach($sec as $s)
	{
		$tmp .= $s.'_';
	}
	$tmp .=0;
	Fset_userdata('str_sec',$tmp);
	Fset_userdata('ctr',$ctr);
	echo $tmp;
}

function aSearchCat()
{
	Fset_userdata('str_cat','');
	$tmp  	= Fget_userdata('str_cat');
	$sec	= '';
	$ctr	= '';
	if(isset($_POST['cat']))
		$sec = $_POST['cat'];
	if(isset($_POST['ctr']))
		$ctr = $_POST['ctr'];
	if($sec != '')	
	foreach($sec as $s)
	{
		$tmp .= $s.'_';
	}
	$tmp .=0;
	Fset_userdata('str_cat',$tmp);
	Fset_userdata('ctr',$ctr);
	echo $tmp;
}
function aSearchprice()
{
	$str_price ='';
	$tmp		='';
	$ctr	= '';
	Fset_userdata('str_price','');
	if(isset($_POST['price']))
		$sec = $_POST['price'];
	if(isset($_POST['ctr']))
		$ctr = $_POST['ctr'];
	foreach($sec as $s)
	{
		$tmp = $s;
	}	
	if($tmp =='101')
		 $str_price = ' AND fprice >= 100000000 ' ;
	else if($tmp =='100')
		 $str_price = ' AND fprice <= 100000000 ' ;
	else if($tmp =='50')
		 $str_price = ' AND fprice <= 50000000 ' ;
	else if($tmp =='30')
		 $str_price = ' AND fprice <= 30000000 ' ;
	else if($tmp =='10')
		 $str_price = ' AND fprice <= 10000000 ' ;
	else if($tmp =='5')
		 $str_price = ' AND fprice <= 5000000 ' ;
	else if($tmp =='0')
		 $str_price = '' ;	 
	Fset_userdata('str_price',$str_price);
	Fset_userdata('chk_price',$tmp);
	Fset_userdata('ctr',$ctr);
	echo $tmp;
}


	
function index()
	{
		$this->do_process();
	}
	
function do_process() 
	{
		$this->get_data(); 		
		$this->caculate_data(); 		
		$this->do_business(); 		
		$this->destroy_data();
	}
	
private function get_data()
	{  
		
	}
	
private function caculate_data()
	{
		//$this->data_product 		= $this->san_pham_model->get_detail_byid($this->nid_products);
		//$this->image_product 		= $this->san_pham_model->get_image_product_byid($this->nid_products);
		
	}
	
private function do_business()
	{	
		$data['data_product'] 		= $this->data_product;
		$data['title']				= $this->title;
		$data['tags']				= $this->tags;
		$data['description']		= $this->description;
		
		$data['nid_cat_work']		= '';
		$data['nid_sec_work']		= '';
		
		$data['menu_top']			= '';
		$data['g_ishome']				= 0;
		$this->load->view('chi_tiet_sp',$data);	
	}
	
private function destroy_data()
	{
				
	}
	

}